﻿using AutoMapper;
using B.S.Warehouse.Domain;
using B.S.Warehouse.ErrorCord;
using B.S.Warehouse.Write.Api.Application.Command.OutboundDetailsCommand;
using MediatR;
using SqlSugar;

namespace B.S.Warehouse.Write.Api.Application.Handelr.OutboundDetailsHandler
{
    public class UpdateOutboundDetailsHandler : IRequestHandler<UpdateOutboundDetailsCommand, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public UpdateOutboundDetailsHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(UpdateOutboundDetailsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.修改成功;
            result.MAG = ApiEnum.修改成功.ToString();
            var model = db.Queryable<OutboundDetailss>().Where(p => p.OutboundDetailsId == request.OutboundDetailsId);
            if (model.Count() != 0)
            {
                var production = mapper.Map<OutboundDetailss>(request);
                await db.Updateable<OutboundDetailss>(production).ExecuteCommandAsync();
            }

            return await Task.FromResult(result);
        }
    }
}
